package com.sky.task;

import com.sky.socket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
public class WebSocketTask {
    @Autowired
    private WebSocketServer webSocketServer;

    /**
     * 通过WebSocket每隔5秒向客户端发送消息
     */
    //@Scheduled(cron = "0/1 * * * * ?")
    public void sendMessageToClient() {
        // 服务器主动给客户端发消息
        // 如果公共聊天，我需要发送存起来的消息
        //webSocketServer.sendToAllClient("这是来自服务端的消息：" + DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalDateTime.now()));
        if (!WebSocketServer.messageList.isEmpty()){
            webSocketServer.sendToAllClient(WebSocketServer.messageList.poll());
        }

    }
}
